#include "Monster.h"

#include "ForwardAI.h"
#include "RandomAI.h"
#include "Spawner.h"
#include "Player.h"


Monster::Monster(int x, int y, const Map& map):
    Movable(x, y, true, Type_Monster)
{
	if (rand() % 2 == 0)
	{
		monsterAI = new ForwardAI(*this, map);
	}
	else
	{
		monsterAI = new RandomAI(*this, map);
	}

	hVelocity = 1;

	setName("Monster");
}


Monster::~Monster()
{
	Spawner::getInstance()->spawn(Spawner::ID_BagOfGold, x, y);

	for (int i = 0; i < getInventoryQuantity(InventoryItem_Key); i++)
	{
		Spawner::getInstance()->spawn(Spawner::ID_Key, x, y);
	}

	for (int i = 0; i < getInventoryQuantity(InventoryItem_GateKey); i++)
	{
		Spawner::getInstance()->spawn(Spawner::ID_GateKey, x, y);
	}

	delete monsterAI;
}


void Monster::getTimedDesiredMove(int& hDirection, int& vDirection)
{
	monsterAI->getMove(hDirection, vDirection);
}


bool Monster::isPassable(const GameObject* gameObject) const
{
	return gameObject->getType() == Type_Player || gameObject->getType() == Type_Projectile;
}


void Monster::handleCollision(GameObject* gameObject)
{
	if (gameObject->getType() == Type_Player)
	{
		Movable* movable = static_cast<Movable*>(gameObject);
		for (int i = 0; i < InventoryItem_Count; ++i)
		{
			InventoryItem item = static_cast<InventoryItem>(i);
			addToInventory(item, movable->getInventoryQuantity(item));
		}

		gameObject->destroy();
	}
}


void Monster::drawObject(int row, int col) const
{
	// Draw monster.
	drawSquare(row, col);
	
	std::vector<drawPoint> points;
	setColor(0,0,0);
	
	glLineWidth(2);

	// Draw eyebrows.
	points.push_back(drawPoint(col + 0.1, row + 0.2, 0));
	points.push_back(drawPoint(col + 0.4, row + 0.5, 0));
	
	points.push_back(drawPoint(col + 0.9, row + 0.2, 0));
	points.push_back(drawPoint(col + 0.6, row + 0.5, 0));

	// Draw mouth.
	points.push_back(drawPoint(col + 0.2, row + 0.75, 0));
	points.push_back(drawPoint(col + 0.8, row + 0.75, 0));

	drawInMode(GL_LINES, points);

	glLineWidth(1);

	points.clear();

	// Draw held item.
	if (hasInInventory(InventoryItem_Key) || hasInInventory(InventoryItem_GateKey))
	{
		// Draw normal key in offhand if both are present.
		if (hasInInventory(InventoryItem_Key) && hasInInventory(InventoryItem_GateKey))
		{
			setColor(0.0f, 1.0f, 1.0f);

			points.push_back(drawPoint(col + 0.9, row + 0.7, 0));
			points.push_back(drawPoint(col + 1.1, row + 0.7, 0));
			points.push_back(drawPoint(col + 1.1, row + 0.9, 0));
			points.push_back(drawPoint(col + 0.9, row + 0.9, 0));

			drawInMode(GL_QUADS, points);

			points.clear();
		}

		// Draw main hand key.
		if (hasInInventory(InventoryItem_GateKey))
		{
			setColor(1.0f, 0.0f, 1.0f);
		}
		else
		{
			setColor(0.0f, 1.0f, 1.0f);
		}

		points.push_back(drawPoint(col - 0.1, row + 0.7, 0));
		points.push_back(drawPoint(col + 0.1, row + 0.7, 0));
		points.push_back(drawPoint(col + 0.1, row + 0.9, 0));
		points.push_back(drawPoint(col - 0.1, row + 0.9, 0));

		drawInMode(GL_QUADS, points);

		points.clear();
	}
}

